
import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';

/// Color 扩展
extension ColorExt on Color {

  /// 将 Color 转换成 MaterialColor
  MaterialColor toMaterialColor() {
    var color = this;
    List strengths = <double>[.05];
    Map swatch = <int, Color>{};
    final int r = color.red, g = color.green, b = color.blue;

    for (int i = 1; i < 10; i++) {
      strengths.add(0.1 * i);
    }
    strengths.forEach((strength) {
      final double ds = 0.5 - strength;
      swatch[(strength * 1000).round()] = Color.fromRGBO(
        r + ((ds < 0 ? r : (255 - r)) * ds).round(),
        g + ((ds < 0 ? g : (255 - g)) * ds).round(),
        b + ((ds < 0 ? b : (255 - b)) * ds).round(),
        1,
      );
    });
    return MaterialColor(color.value, swatch);
  }

}

/// 字符串扩展
extension StringExt on String {

  /// 获取展示互动房间号
  String getGenseeNumber() {
    int index = this.lastIndexOf('/');
    if (index >= 0 && index < this.length) {
      return this.substring(index + 1);
    }

    return null;
  }
}

/// 文字样式扩展，
extension TextStyleExt on TextStyle {

  /// 高度包裹内容
  /// height = 1 , 这样 Text 的实际高度为 fontSize * height，
  /// 因为默认的文字有上边距，可能会出现文字偏下的情况，尤其是和图片在一行时，对齐不了
  TextStyle wrapHeight() {
    return this.copyWith(height: 1);
  }
}

/// 日历格式化扩展，
extension DateTimeExt on DateTime {
  /// 2020-02-02
  String commonFormat() {
    return formatDate(this, ['yyyy', '-', 'mm', '-', 'dd',]);
  }

  /// 2020年02月02日
  String commonFormatChina() {
    return formatDate(this, ['yyyy', '年', 'mm', '月', 'dd', '日',]);
  }

  bool isSameDay(DateTime other) {
    return this.year == other.year && this.month == other.month && this.day == other.day;
  }

}
